package hadoop.partition;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class PartitionJob {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJarByClass(PartitionJob.class);
        job.setMapperClass(PartitionMapper.class);
        job.setReducerClass(PartitionReducer.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
//        在job端指定reduceTask的数量和自定义分区。
        job.setNumReduceTasks(3);
        job.setPartitionerClass(MyPartitioner.class);

        FileSystem fs = FileSystem.get(conf);
//        Path path = new Path("hdfs://master:9000//wordcount");
        Path path = new Path("D:\\hadoopwork\\mapreduce\\output2");
        if (fs.exists(path)){
            fs.delete(path,true);
        }
//        FileInputFormat.setInputPaths(job,new Path("hdfs://master:9000//input//wordcount.txt"));
        FileInputFormat.setInputPaths(job,new Path("D:\\hadoopwork\\mapreduce\\input\\wordcount.txt"));
        FileOutputFormat.setOutputPath(job,path);
        boolean isDone = job.waitForCompletion(true);
        System.exit(isDone ? 0 : 1);
    }

}

